package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.StatusConstant;
import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.entity.Setmeal;
import com.sky.entity.SetmealDish;
import com.sky.mapper.SetmealDishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.result.PageResult;
import com.sky.service.SetmealService;
import com.sky.vo.DishItemVO;
import com.sky.vo.DishVO;
import com.sky.vo.SetmealVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class SetmealServiceImpl implements SetmealService {

    @Autowired
    private SetmealMapper setmealMapper;
    @Autowired
    private SetmealDishMapper setmealDishMapper;

    /**
     * 根据分类id查看套餐
     * @param categoryId
     * @return
     */
    public List<Setmeal> list(Long categoryId) {
        Setmeal setmeal = Setmeal.builder()
                .categoryId(categoryId)
                .status(StatusConstant.ENABLE)
                .build();

        List<Setmeal> setmeals = setmealMapper.selectByCatId(setmeal);
        return setmeals;
    }

    /**
     * 根据套餐id查看包含的菜品
     * @param id
     * @return
     */
    public List<DishItemVO> dishList(Long id) {
        List<DishItemVO> dishItemVOS = setmealMapper.selectBySetmealId(id);
        return dishItemVOS;
    }

    /**
     * 套餐分页查询
     * @param setmealPageQueryDTO
     * @return
     */
    public PageResult save(SetmealPageQueryDTO setmealPageQueryDTO) {
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealPageQueryDTO, setmeal);

        PageHelper.startPage(setmealPageQueryDTO.getPage(), setmealPageQueryDTO.getPageSize());

        List<Setmeal> setmeals = setmealMapper.pageFind(setmeal);
        Page page = (Page) setmeals;
        return new PageResult(page.getTotal(), page);
    }

    /**
     * 根据id查询套餐
     * @param id
     * @return
     */
    public SetmealVO findById(Long id) {
        Setmeal setmeal = new Setmeal();
        setmeal.setId(id);
        Setmeal selectById = setmealMapper.selectById(setmeal);
        List<SetmealDish> selectBySetmealId = setmealDishMapper.selectBySetmealId(setmeal);
        SetmealVO setmealVO = new SetmealVO();
        BeanUtils.copyProperties(selectById, setmealVO);
        setmealVO.setSetmealDishes(selectBySetmealId);
        return setmealVO;
    }

    /**
     * 新增套餐
     * @param setmealDTO
     */
    public void addSetmeal(SetmealDTO setmealDTO) {
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO, setmeal);
//        插入到套餐表
        setmealMapper.insert(setmeal);


        List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();
        for (SetmealDish dish : setmealDishes) {
            SetmealDish setmealDish = new SetmealDish();
            BeanUtils.copyProperties(dish, setmealDish);
            setmealDish.setSetmealId(setmeal.getId());
//            插入到套餐菜品表
            setmealDishMapper.insert(setmealDish);
        }
    }

    /**
     * 修改套餐
     * @param setmealDTO
     */
    public void modifySetmeal(SetmealDTO setmealDTO) {
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO, setmeal);
        setmealMapper.update(setmeal);

//        更新套餐菜品表
        List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();
        for (SetmealDish setmealDish : setmealDishes) {
            SetmealDish setmealDish1 = new SetmealDish();
            BeanUtils.copyProperties(setmealDish, setmealDish1);
            setmealDish1.setSetmealId(setmeal.getId());
            setmealDishMapper.update(setmealDish1);
        }

    }

    /**
     * 启用或者禁用套餐
     * @param status
     * @param id
     */
    public void startOrStop(Integer status, Long id) {
        Setmeal setmeal = Setmeal.builder()
                .status(status)
                .id(id)
                .build();
        setmealMapper.update(setmeal);
    }

    /**
     * 根据id删除套餐
     * @param ids
     */
    public void dropSetmeal(List<Long> ids) {
//            删除套餐表数据
            setmealMapper.deleteSet(ids);
//            删除套餐关联 套餐菜品表数据
        setmealDishMapper.deleteSet(ids);
    }
}
